package com.huifaapp.mall.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.huifaapp.mall.R;
import com.huifaapp.mall.bean.PendingCommBean;

import java.util.List;

/**
 * Created by mac on 2017/12/27.
 */

public class PendingCommAdaptertwo extends RecyclerView.Adapter<PendingCommAdaptertwo.MyViewHolder> {
    private Context context;
    private List<PendingCommBean.OrderListBean.DataBean> data;

    public PendingCommAdaptertwo(Context context, List<PendingCommBean.OrderListBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.waitpay_item11, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        PendingCommBean.OrderListBean.DataBean dataBean = data.get(position);   //获取当前条目的数据
        Glide.with(context).load(dataBean.getGoodsImg()).into(holder.pendpay_im);   //加载商品图片

        holder.pendpay_shopname.setText(dataBean.getGoodsName());   //商品的名字
        holder.pendpay_xj.setText("¥" + dataBean.getPrice());   //商品的现价
        holder.pendpay_yj.setText("¥" + dataBean.getOrginPrice());   //商品的原价
        holder.pendpay_yj.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);   //给商品原价的TextView添加删除线
        holder.pendpay_num.setText("x" + dataBean.getCount());   //商品的个数

        PendingCommBean.OrderListBean.DataBean.ParamBean param = dataBean.getParam();
        String color = param.get颜色() == null ? "无" : param.get颜色();   //三元运算符   判断param.getColor()是否为null  如果是null则取 : 前面的值，否则后面的值
        String brand = param.get尺寸() == null ? "**" : param.get尺寸();   //同上
        holder.tv_color.setText("颜色分类:" + color + "；");   //商品的颜色
        holder.tv_size.setText("尺寸:" + brand);   //商品的尺寸


        if (onItemClickListener !=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.OnItem(holder.itemView, holder.getLayoutPosition());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView pendpay_im;
        TextView pendpay_shopname, pendpay_xj, pendpay_yj, pendpay_num, tv_color, tv_size;

        public MyViewHolder(View itemView) {
            super(itemView);
            pendpay_im = (ImageView) itemView.findViewById(R.id.pendpay_im);
            pendpay_shopname = (TextView) itemView.findViewById(R.id.pendpay_shopname);
            pendpay_xj = (TextView) itemView.findViewById(R.id.pendpay_xj);
            pendpay_yj = (TextView) itemView.findViewById(R.id.pendpay_yj);
            pendpay_num = (TextView) itemView.findViewById(R.id.pendpay_num);
            tv_color = (TextView) itemView.findViewById(R.id.tv_color);
            tv_size = (TextView) itemView.findViewById(R.id.tv_size);

        }
    }

    public interface OnItemClickListenertwo {
        void OnItem(View view,int position);
    }

    private PendpayAdaptertwo.OnItemClickListenertwo onItemClickListener;

    public void setOnItemClickListener(PendpayAdaptertwo.OnItemClickListenertwo onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

}
